package com.qiaoba.module.job.controller;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.job.entity.SysJobLog;
import com.qiaoba.module.job.entity.param.SysJobLogParam;
import com.qiaoba.module.job.service.SysJobLogService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 定时任务日志 Web层
 *
 * @author ailanyin
 * @date 2023-09-04 14:18:12
 */
@Validated
@RestController
@RequestMapping("/monitor/jobLog")
@RequiredArgsConstructor
@Tag(name = "定时任务日志")
public class SysJobLogController {

    private final SysJobLogService sysJobLogService;

    @PreAuthorize("hasAuthority('monitor:job:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public PageResult list(@Validated SysJobLogParam param, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysJobLog> list = sysJobLogService.selectList(param);
        return PageResult.build(list);
    }

    @PreAuthorize("hasAuthority('monitor:job:export')")
    @PostMapping("/export")
    @Operation(summary = "导出Excel")
    public void export(HttpServletResponse response, SysJobLogParam param) {
        List<SysJobLog> list = sysJobLogService.selectList(param);
        ExcelUtil.exportExcel(list, SysJobLog.class, "定时任务日志", response);
    }

    @DeleteMapping("/{jobLogIds}")
    @Operation(summary = "删除日志")
    public Result remove(@PathVariable String[] jobLogIds) {
        return Result.toAjax(sysJobLogService.deleteByIds(CollUtil.toList(jobLogIds)));
    }

    @DeleteMapping("/clean")
    @Operation(summary = "清空日志")
    public Result clean() {
        sysJobLogService.cleanAll();
        return Result.success();
    }
}
